<%-- 
    Document   : deleteEmployee
    Created on : May 5, 2013, 11:40:58 PM
    Author     : Byung Park
--%>

<%@page import="java.sql.ResultSet"%>
<%@page import="db.UserDAO"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="db.DB"%>
<%@page import="java.io.*"%>
<%

    String name = request.getParameter("name");
    String type = request.getParameter("type");
    String rating = request.getParameter("rating");
    String distr = request.getParameter("distr");
    String copies = request.getParameter("copies");
    String description = request.getParameter("description");
    String srcFileName = request.getParameter("filename");
    
    try {
        DB data = new DB();
        Connection conn = data.getConnection();
        Statement st = null;
        Statement st1 = null;
        st = conn.createStatement();
        st1 = conn.createStatement();
        String s = application.getRealPath("/");
        String query = "INSERT INTO Movie (Name,Type,Rating,DistrFee,NumCopies) VALUES ('" + name + "','" + type + "','" + rating + "','" + distr + "','" + copies + "');";

        
        st.execute(query);
        
        ResultSet rs = st1.executeQuery("SELECT LAST_INSERT_ID()");
   
        String id = "0";
        while (rs.next()) {
            id = rs.getString(1);
        }
        
        String dstFileName = s+"images/movies/" + type + "/movie" + id + ".jpg";
        
        File srcFile = new File(srcFileName);
        File dstFile = new File(dstFileName);
        
        
        
        FileInputStream srcStrm = new FileInputStream(srcFile);
        FileOutputStream dstStrm = new FileOutputStream(dstFile);
        
        byte[] buf = new byte[4096];
        int len;
        while ((len = srcStrm.read(buf)) > 0) {
            dstStrm.write(buf, 0, len);
        }
        srcStrm.close();
        dstStrm.close();
        
        String path = s + "images/movies/" + type + "/movie" + id + "Desc.txt";
        //out.println(path);
        
        File f1 = new File(path);
        FileWriter fw = new FileWriter(f1);
        
        Writer objWriter = new BufferedWriter(new FileWriter(f1));
        objWriter.write(description);
        
        objWriter.flush();
        objWriter.close();
        
        response.sendRedirect("movieList.jsp");

        }  catch (Exception e) {
    };
%>

